let hasPermission = function (role, route) {
  if (route.meta && route.meta.roles) {
    return route.meta.roles.includes(role)
  } else {
    return true
  }
}

export function filterAsyncRoutes(routes, role) {
  const res = []
  routes.forEach((route) => {
    const tmp = { ...route }
    if (hasPermission(role, tmp)) {
      if (tmp.children) {
        tmp.children = filterAsyncRoutes(tmp.children, role)
      }
      if (tmp.children && tmp.children.length > 0) {
        tmp.redirect = () => ({
          name: tmp.children[0].name,
        })
        res.push(tmp)
      } else if (!tmp.children) {
        res.push(tmp)
      }
    }
  })
  return res
}
